package com.atguigu.webdemo.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.webdemo.bean.Customer;
import com.atguigu.webdemo.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {

    //测试链接
    @RequestMapping(value = "seyhello")
    public String seyHello (){
        System.out.println("请求发送成功");
        return "请求发送成功!!!";
    }

    //键值对参数
    @RequestMapping(value = "param1")
    public String param1(@RequestParam(value = "name",required = false) String name,@RequestParam(value ="age" , required = false) Integer age){
        System.out.println("RequestParam: "+name+"::"+age);
        return name+"::"+age;
    }

    //拼接请求URL中的参数
    @RequestMapping("/param2/{name}/{age}")
    public String param2(@PathVariable("name") String name , @PathVariable("age") int age){
        System.out.println("PathVariable: "+name+"::"+age);
        return name+"::"+age;
    }

    //请求体参数
    @RequestMapping("param3")
    public Customer param3(@RequestBody Customer customer){

        System.out.println("customer = " + customer);

        return customer;
    }

    //请求方式
    //@GetMapping("param4")
    @PostMapping("param4")
    public String param4(){
        System.out.println("连接成功!!");
        return "连接成功";
    }

    //调用业务层
    @Autowired
    @Qualifier("customerServiceimpl")
    CustomerService customerService;

    @RequestMapping("param5/{id}")
    public Customer param5(@PathVariable("id") String id){

        //Customer customer = customerService.getCustomer(id);
        //customerService.getCustomer(id)
        //return customerService.getCustomer(id);
        return customerService.getById(id);

    }

    @PostMapping("paramUpdate")
    public void paramUpdate(@RequestBody Customer customer){
        //customerService.updateCustomerService(customer);
        customerService.updateById(customer);
    }

    @PostMapping("paeaminsert")
    public void paraminsert(@RequestBody Customer customer){
        //customerService.insertCustomerService(customer);
        customerService.save(customer);
    }

    @RequestMapping("paramdelete/{id}")
    public void paramdelete(@PathVariable("id") String id){
        //customerService.deleteCustomerService(id);
        customerService.remove(
                new QueryWrapper<Customer>().eq("id",id)
        );
    }

    @RequestMapping("test")
    public String paramtest(@RequestParam(value = "name",required = false) String name ,@RequestParam(value = "age",required = false) Integer age){
        List<Customer> list = customerService.list(
                new QueryWrapper<Customer>().eq(age != null, "age", age).eq(name != null, "username", name)
        );
        return JSON.toJSONString(list);
    }


}
